/**
 * Contact数据传输对象
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.dto;


import lombok.Data;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.sxf.crm.entity.Contact;
import com.sxf.crm.entity.Department;
import com.sxf.crm.enums.DeleteStatus;

/**
 * 联系人数据传输对象
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/6/1
 */
@Data
public class ContactDTO {
    private Long id;
    private String name;
    private String phone;
    private String email;
    private String position;
    private String remark;
    private Long customerId;
    private String customerName;
    private List<Long> departmentIds;
    private List<String> departmentNames;
    private Set<Department> departments;
    private String deleteStatus;
    private String deleteStatusLabel;
    private Long createAt;
    private String creatorName;
    private Long createUserId;
    private Long updateAt;
    private String updaterName;
    private Long updateUserId;

    public static ContactDTO fromEntity(Contact contact) {
        if (contact == null) {
            return null;
        }

        ContactDTO dto = new ContactDTO();
        dto.setId(contact.getId());
        dto.setName(contact.getName());
        dto.setPhone(contact.getPhone());
        dto.setEmail(contact.getEmail());
        dto.setPosition(contact.getPosition());
        dto.setRemark(contact.getRemark());
        dto.setRemark(contact.getRemark());
        
        if (contact.getCustomer() != null) {
            dto.setCustomerId(contact.getCustomer().getId());
            dto.setCustomerName(contact.getCustomer().getName());
        }

        dto.setDepartments(contact.getDepartments());
        
        if (contact.getDepartments() != null) {
            dto.setDepartmentIds(contact.getDepartments().stream()
                .map(Department::getId)
                .collect(Collectors.toList()));
            dto.setDepartmentNames(contact.getDepartments().stream()
                .map(Department::getName)
                .collect(Collectors.toList()));
        }
        
        dto.setDeleteStatus(contact.getDeleteStatus().name());
        dto.setDeleteStatusLabel(contact.getDeleteStatus() == DeleteStatus.DELETED ? "已删除" : "未删除");
        
        dto.setCreateAt(contact.getCreateAt());
        dto.setCreateUserId(contact.getCreateUserId());
        dto.setUpdateAt(contact.getUpdateAt());
        dto.setUpdateUserId(contact.getUpdateUserId());
        
        return dto;
    }
} 